package tripx.server;

import java.io.IOException;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

import tripx.system.Logger;

/**
 * <code><b>TripxServer</b></code> starts a xml rpc service.
 * 
 * @author zhb
 * 
 */
public class TripxServer {

	private WebServer server;

	private PropertyHandlerMapping pMapping;

	private Logger logger;

	public TripxServer(PropertyHandlerMapping pMapping, int port) {
		server = new WebServer(port);
		this.pMapping = pMapping;
		logger = Logger.getInstance();
	}

	public void start() {
		XmlRpcServerConfigImpl config = new XmlRpcServerConfigImpl();
		config.setEnabledForExtensions(true);
		server.getXmlRpcServer().setConfig(config);
		server.getXmlRpcServer().setHandlerMapping(pMapping);
		try {
			server.start();
			logger.info("Server started...");
		} catch (IOException e) {
			logger.err("Server can not be started...");
		}
	}

	public void shutdown() {
		server.shutdown();
		logger.info("Server shuted...");
	}
}
